/**
 * 
 */
package userService;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.jws.Oneway;

import constants.Constants;


/**
 * @author Yokesh Kumar
 *
 */
public class CustomerService {

	private Connection connection;
	private PreparedStatement statement;
	
	public CustomerService(){
		try {
			Class.forName(Constants.JDBC_DRIVER);
			connection = DriverManager.getConnection(Constants.DB_URL,Constants.USER_NAME,Constants.PWD);
		} 
		catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	/**
	 * This method fetches the reservation details of the customer
	 * from reservation table
	 * @param phoneNumber
	 * @return
	 * @throws SQLException 
	 */
	public ResultSet fetchReservationDetails(String phoneNumber) throws SQLException{
		ResultSet rs = null;
		String query = "select reservationId,reservationDate,status from reservation where customerPhoneNumber = ? and status != ?";
		try {
			statement = connection.prepareStatement(query);
			statement.setString(1, phoneNumber);
			statement.setString(2, Constants.RESERVATION_RENTED);
			rs =  statement.executeQuery();
		} 
		catch (SQLException e1) {
			e1.printStackTrace();
			throw e1;
		}
		return rs;
	}
	
	/**
	 * This method fetches the rent details of the customer
	 * from rent table
	 * @param phoneNumber
	 * @return
	 * @throws SQLException 
	 */
	public ResultSet fetchRentDetails(String phoneNumber) throws SQLException{
		ResultSet rs = null;
		String query = "select r.reservationId,r.reservationDate,r.status,re.rentId,re.rentTime,re.rentDate,rt.returnDate,rt.returnTime,v.vehicleName,rc.finalCost from reservation r, rent re, return_vehicle rt, vehicle v, return_cost rc  where r.reservationId = re.reserveId and re.rentId = rt.returnId and re.vehicleId = v.vehicleId and rt.paymentId = rc.returnPaymentId and r.customerPhoneNumber = ?";
		try {
			statement = connection.prepareStatement(query);
			statement.setString(1, phoneNumber);
			rs =  statement.executeQuery();
		} 
		catch (SQLException e1) {
			e1.printStackTrace();
			throw e1;
		}
		return rs;
	}
	
	/**
	 * This method fetches the membership details of the customer
	 * from membership tables
	 * @param phoneNumber
	 * @return
	 * @throws SQLException 
	 */
	public ResultSet fetchMembershipDetails(String phoneNumber) throws SQLException{
		ResultSet rs = null;
		String query = "SELECT * FROM membership WHERE phoneNumber=? AND status!=?";
		try {
			statement = connection.prepareStatement(query);
			statement.setString(1, phoneNumber);
			statement.setString(2,Constants.MEMBERSHIP_INACTIVE);
			rs =  statement.executeQuery();
		} 
		catch (SQLException e1) {
			e1.printStackTrace();
			throw e1;
		}
		return rs;
	}
	
	public void closeConnection(){
		try{
			if(statement != null){
				statement.close();
			}
			if(connection != null){
				connection.close();
			}
		}
		catch(SQLException sqlExec){
			sqlExec.printStackTrace();
		}
	}
	/**
	 * @param phoneNumber
	 * @throws SQLException 
	 */
	public ResultSet checkForMembership(String phoneNumber) throws SQLException {
		ResultSet rs = null;
		System.out.println(phoneNumber);
		String query = "SELECT isMember FROM customer WHERE phoneNumber=? ";
		try {
			statement = connection.prepareStatement(query);
			statement.setString(1, phoneNumber);
			rs =  statement.executeQuery();
		} 
		catch (SQLException e1) {
			e1.printStackTrace();
			throw e1;
			
		}
		return rs;
		
	}
}
